package com.epalmpay.controller.apimemberapp;


import com.epalmpay.bean.OilcardRechargeInfoBean;
import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.profitmanage.IProfitManageService;
import com.epalmpay.service.ys.memberRedActivityService.IYsMemberRedActivityPerfectService;
import com.epalmpay.service.yunstream.*;
import com.epalmpay.usercenter.UserAuthApi;
import com.epalmpay.util.Response;
import com.epalmpay.util.payplat.SecurityUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import javax.annotation.Resource;


/**
 * 
* 类名称：ApiMemberAppYSController.java
* 类描述： 云流 controller
* @author tony
* 创建时间：2017年8月3日
 */
@Controller
@RequestMapping(value="/api/memberapp/ys")
public class ApiMemberAppThirdYSController extends BaseController{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private IObdService obdService;
	@Resource
	private IProfitManageService profitManageService;

	@Resource
	private IYsMemberRedActivityPerfectService ysMemberRedActivityPerfectService;

	/**
	 * obd设备激活
	 * @param groupId
	 * @param userId
	 * @param app_member_sid
	 * @return
	 */
	@RequestMapping(value = "/obdActivate", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response obdActivate(@RequestParam(required = true) String memberId,
								@RequestParam(required = true) String deviceId,
								@RequestParam(required = true) String activateTime,
								@RequestParam(required = true) String sign){

		logBefore(logger, "obd设备激活");

		Map<String, Object> map = null;
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(memberId).append(deviceId).append(activateTime).append(UserAuthApi.md5Key);
			String localSign = SecurityUtil.md5LowerCase(sb.toString());
			if(!sign.equalsIgnoreCase(localSign)){
				System.out.println("sign 不正确！:"+localSign);
				//throw new ApplicationException("sign 不正确！");
			}

			obdService.updateObdActivate(memberId,deviceId,activateTime,sign);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			ae.printStackTrace();
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			return Response.getError("服务器异常");
		}

	}

	/**
	 * 保单分润
	 * @param groupId
	 * @param userId
	 * @param app_member_sid
	 * @return
	 */
	@RequestMapping(value = "/policyProfitCalculate", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response obdActivate(@RequestParam(required = true) String policyId,
								@RequestParam(required = true) String memberId,
								@RequestParam(required = true) Integer orderAmount,
								@RequestParam(required = true) String areaCode,
								@RequestParam(required = false) String areaName,
								@RequestParam(required = false) String idNo,
								@RequestParam(required = false) String confirmTime,
								@RequestParam(required = false) Integer policyStatus,
								@RequestParam(required = true) String sign){

		logBefore(logger, "obd设备激活");

		Map<String, Object> map = null;
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(policyId).append(memberId).append(orderAmount).append(areaCode).append(UserAuthApi.md5Key);
			String localSign = SecurityUtil.md5LowerCase(sb.toString());
			if(!sign.equalsIgnoreCase(localSign)){
				System.out.println("sign 不正确！:"+localSign);
				//throw new ApplicationException("sign 不正确！");
			}

			profitManageService.insertPolicyProfitCalculate(policyId,memberId,orderAmount,areaCode,areaName,idNo,confirmTime,policyStatus);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}

	}

	/**
	 * 会员3证认证通过通知
	 * @param memberId
	 * @param verityStatus
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/member3VerityNotify", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Response member3VerityNotify(
								@RequestParam(required = true) String memberId,
								@RequestParam(required = false) String verityStatus,
								@RequestParam(required = true) String sign){

		logBefore(logger, "obd设备激活");

		Map<String, Object> map = null;
		try {
//			StringBuffer sb = new StringBuffer();
//			sb.append(memberId).append(verityStatus).append(UserAuthApi.md5Key);
//			String localSign = SecurityUtil.md5LowerCase(sb.toString());
//			if(!sign.equalsIgnoreCase(localSign)){
//				System.out.println("sign 不正确！:"+localSign);
//				throw new ApplicationException("sign 不正确！");
//			}

			ysMemberRedActivityPerfectService.insertMemberRedActivityPerfectInfo(memberId,verityStatus);
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}

	}




}
